package com.example.chatgptapplication.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.example.chatgptapplication.entity.User;
import com.example.chatgptapplication.mapper.UserMapper;


import com.example.chatgptapplication.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {



    @Override
    public User userLogin(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account",user.getAccount());
        queryWrapper.eq("password",user.getPassword());
        User u = getOne(queryWrapper);
        return u;
    }

    @Override
    @Transactional
    public void createUser(User user) {
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        user.setStatus(1);
        save(user);


    }


    @Override
    public void updateUser(User user) {
        this.updateById(user);
    }

    @Override
    public User getUserInfoById(String userId) {
        return this.getById(userId);
    }

    @Override
    public User registerUser(User user) {
        user.setStatus(1);
        //设置默认头像
        user.setAvatar("https://img0.baidu.com/it/u=3204281136,1911957924&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500");
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        this.save(user);
        return user;
    }

    @Override
    public void forbidden(User user) {
        user.setStatus(0);
        this.updateById(user);
    }

    @Override
    public void enable(User user) {
        user.setStatus(1);
        this.updateById(user);
    }

    @Override
    public Page<User> getUserPageData(User user) {
        Page<User> platformPage = getPageData(user);
        QueryWrapper<User> queryWrapper = getQueryData(user);
        Page<User> page = this.page(platformPage,queryWrapper);
        List<User> users = page.getRecords().stream().map(item -> {

            return item;
        }).collect(Collectors.toList());
        page.setRecords(users);
        return page;
    }


    public Page<User> getPageData(User user){
        Integer pageNum = user.getPageNum();
        Integer pageSize = user.getPageSize();
        Page<User> page = new Page<>(pageNum,pageSize);
        return page;
    }


    public QueryWrapper<User> getQueryData(User user){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if(user.getAccount() != null && !user.getAccount().equals("")){
            queryWrapper.eq("account",user.getAccount());
        }
        return queryWrapper;
    }
}
